#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <atomic>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/time.h>
#include <boost/algorithm/string.hpp>


const std::string temppath="./temp/";
namespace ns_util
{
    class TimeUtil
    {
        public:
        static std::string GetTimeStamp()
        {
            struct timeval _time;
            gettimeofday(&_time,nullptr);
            return std::to_string(_time.tv_sec);
        }
        static std::string GetTimeMs()
        {
            struct timeval _time;
            gettimeofday(&_time, nullptr);
            return std::to_string(_time.tv_sec * 1000 + _time.tv_usec / 1000);
        }


    };
    class PathUtil
    {
      public:
        static std::string AddPath(const std::string &file,const::std::string&appendix)
        {
            std::string pathname=temppath+file;
            pathname+=appendix;
            return pathname;
        }
        static std::string Src(const std::string &file)
        {
            return AddPath(file,".cpp");
        }
        static std::string Exe(const std::string &file)
        {
            return AddPath(file,".exe");
        }
        static std::string CompileError(std::string file)
        {
            return AddPath(file,".compile_err");
        }
        //运行时需要的临时文件
        static std::string Stdout(std::string file)
        {
            return AddPath(file,".stdout");
        }
        static std::string Stdin(std::string file)
        {
            return AddPath(file,".stdin");
        }
        //标准错误路径
        static std::string Stderr(std::string file)
        {
            return AddPath(file,".stderr");
        }
    };
    class FileUtile
    {
        public:
        static bool IsExist(const std::string &file)
        {
            struct stat buffer;
            return (stat (file.c_str(), &buffer) == 0); 
        }
        static std::string UniqFileName()
        {
            static std::atomic_uint id(0);
            id++;
            std::string ms = TimeUtil::GetTimeMs();
            std::string uniq_id = std::to_string(id);
            return ms + "_" + uniq_id;

        }
        static bool WriteFile(const std::string &target, const std::string &content)
        {
            std::ofstream out(target); 
            if(!out.is_open())
            {
                return false;
            }
            out.write(content.c_str(),content.size());
            out.close();
            return true;
        }
        static bool ReadFile(const std::string &target, std::string *content,bool keep=false)
        {
            (*content).clear();
            std::ifstream in(target); 
            if(!in.is_open())
            {
                return false;
            }
            std::string line;
            while (std::getline(in, line))
            {
                // getline:不保存行分割符,有些时候需要保留\n,
                // getline内部重载了强制类型转化
                (*content)+=line;
                if(keep)
                {
                    (*content)+='\n';
                }
                else
                {
                    (*content)+=' ';
                }
            }
            in.close();
            return true;
        }
    };
    class StringUtil
    {
        public:
        static void Split(const std::string &str, std::vector<std::string>*target ,const std::string &delim)
        {
            boost::split((*target),str,boost::is_any_of(delim),boost::algorithm::token_compress_on);
        }
    };
}

